package top.zhaoyuliu.logbook.client.utils;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

/**
 * Copyright © 2021 Arain. All rights reserved.
 *
 * @Title: AppUtils.java
 * @Prject: logbook-client
 * @Package: top.zhaoyuliu.logbook.client.utils
 * @Description: <功能详细描述>
 * @author: Arain
 * @date: 2021年1月22日 下午7:13:29
 * @version: V1.0
 */
public class AppUtil {

	public static String getIp() {
		String ip = "";
		try {
			InetAddress ip4 = Inet4Address.getLocalHost();
			if (null != ip4) {
				ip = ip4.getHostAddress();
			} else {
				InetAddress ipv6 = Inet6Address.getLocalHost();
				if (null != ipv6) {
					ip = ipv6.getHostAddress();
				}
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return ip;
	}

	
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip.equals("0:0:0:0:0:0:0:1")) {
			ip = "本地";
		}
		return ip;
	}
}
